"""
time: 2024/10/24
title: StringOperation
message:
密钥处理
"""
import random


class StringOperation:
    def __init__(self):
        pass

    @staticmethod
    def encrypt(text):
        """
        加密
        :param text:
        :return:
        """
        try:
            ran = random.randint(1, 45)
            if ran < 10:
                ran_s = f"0{ran}"
            else:
                ran_s = ran
            encrypted_text = ""
            for char in text:
                encrypted_text += chr(ord(char) + ran)  # 替换为ASCII码加 ran 后的字符
            scri = encrypted_text + f"{ran_s}"
            if not any(cell in ['"', '"', '“', "‘", "`"] for cell in scri):
                return str(scri)
            else:
                return StringOperation().encrypt(text)
        except BaseException as e:
            print(f"密钥生成时发生错误, {e}")
            return StringOperation().encrypt(text)

    @staticmethod
    def decrypt(encrypted_text):
        """
        解密
        :param encrypted_text:
        :return:
        """
        ran_s = encrypted_text[-2:]
        text = ""
        # print(ran_s)
        for char in encrypted_text:
            text += chr(ord(char) - int(ran_s))  # 替换为ASCII码减n后的字符
        return text[:-2]


if __name__ == '__main__':
    p = StringOperation().encrypt("abcdefghijklmnopqrstuvwxyz1234567890")
    print(p)
    p = '\u0089z\u0088\u0089GEGIFEGI\u0091FGLCECECF\u0091GEGIBFFBEK5FIOHIOFL21'
    r = StringOperation().decrypt(p)
    print(r)
